var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    info_find:'',
    project:[],
    totalPrice:0,
    select_project:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
    this.info()
    console.log(options)
  },

  info(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/package_info",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{id:that.data.id},
      success: function(res) {
        console.log(res)
        that.setData({
          info_find:res.data,
          project:res.data.project
        })
        var article = res.data.content
        WxParse.wxParse('article', 'html', article, that, 5)
      }
    })
  },

  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let project = this.data.project;
    const selected = project[index].selected;
    project[index].selected = !selected;  
    this.setData({
      project:project
    });
    this.jisuan()
  },

  xieyi(){
    wx.navigateTo({
      url: '/pages/xieyi/xieyi?type=3',
    })
  },

  checkOut(){
    var that = this
    if(this.data.select_project.length==0){
      wx.showToast({
        title: '请选择项目',
      })
      return false
    }else{
      var project_id = '';
      var project = this.data.project
      for(var i=0;i<project.length;i++){
        if(project[i]['selected']){
          project_id += project[i]['id']+','
        }
      }
    }
    
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/insert_cart2",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{id:that.data.id,project_id:project_id,user_id:getApp().globalData.userInfo.id},
      success: function(res) {
        wx.navigateTo({
          url: '/pages/pay/index?type=2&ids='+'',
        })
      }
    })
  },

  toSelect(){
    var that = this
    if(this.data.select_project.length==0){
      wx.showToast({
        title: '请选择项目',
      })
      return false
    }else{
      var project_id = '';
      var project = this.data.project
      for(var i=0;i<project.length;i++){
        if(project[i]['selected']){
          project_id += project[i]['id']+','
        }
      }
    }
    
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/insert_cart",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{id:that.data.id,project_id:project_id,user_id:getApp().globalData.userInfo.id},
      success: function(res) {
        wx.showToast({
          title: '加入购物车成功',
        })
        app.cart_count()
      }
    })
  },

  jisuan(){
    var project = this.data.project
    var total_price = 0
    var select_project = []

    for(var i=0;i<project.length;i++){
      if(project[i]['selected']){
        total_price += Number(project[i]['price'])
        select_project.push(project[i]['id'])
      }
    }
    this.setData({
      totalPrice:total_price,
      select_project:select_project
    })
    console.log(this.data.select_project)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})